/**
 * Created by sunyan on 2017/8/14.
 */
const webpage = require('webpage')

var width = 1800;
var height = 1800;

//时间
var today = new Date();
var year = today.getFullYear();    //年
var month = today.getMonth() + 1;  //月
var day = today.getDate();         //日
var week = today.getDay()          //周
var hour = today.getHours()        //时
var minutes = today.getMinutes()   //分

var page = webpage.create()
    page.viewportSize = {width: width, height: height}
dateObj = new dateObj()
function screen_shot_2() {
  var beginDate = defaultConfig().beginDate,
      endDate   = defaultConfig().endDate
  page.open('http://121.42.252.26/operation/pages/user-login.html?beginDate='+beginDate+'&endDate='+endDate, function(status) {  //打开登陆
    console.log("Status: " + status);
    if(status === "success") {
      page.evaluate(function(w, h) {
        //设置body大小
        document.body.style.width = w + "px";
        document.body.style.height = h + "px";
        //模拟登陆
        document.getElementById('userName').value = 'liuxi'
        document.getElementById('password').value = 'yxb123YXB'
        Login_2()
      })

      //延迟截图, 由于接口太慢，等待5分钟再截图
      setTimeout(function () {
        console.log('截图')
        page.clipRect = { top: 165, left: 273, width: width, height: height };
        page.render('chart_images/chart_report_key_'+defaultConfig().fileName+'.png');
        phantom.exit();
      }, 60000);
    }
  })
  page.onUrlChanged = function(targetUrl) {
    console.log('New URL: ' + targetUrl);
  };
}
screen_shot_2()

/*
 * @getLastWeekTime 获取一周以前的日期
 * @getLastMonthTime 获取上个月的日期
 * */
function dateObj(){
  this.getLastWeekTime = function (){
    var current=today.getTime();
    var minusDate=1000*60*60*24*7;
    var time = new Date(current-minusDate);
    var yearW = time.getFullYear(),
      monthW = time.getMonth()+1,
      dayW = time.getDate()
    var dateStr = yearW +'-'+ (monthW>=10?monthW:'0'+monthW)+'-'+dayW
    var obj = {
      year: yearW,
      month:monthW,
      day:dayW,
      dateStr:dateStr
    }
    return obj
  }
  this.getLastMonthTime = function () {  //获取上个月第一天和最后一天
    var yearM = today.getFullYear();
    var monthM = today.getMonth();
    if (monthM==0) {
      monthM=12;
      yearM=yearM-1;
    }
    if (monthM < 10) {
      monthM = "0" + monthM;
    }
    var firstDay = yearM + "-" + monthM + "-" + "01"; //上个月的第一天
    var lastDay = year + "-" + (month>=10?month:'0'+month) + "-" + '01'; //本月一天
    var obj = {
      year:yearM,
      month:monthM,
      firstDay:firstDay,
      lastDay:lastDay,
    }
    return obj
  }
}
/*
 * 基本配置
 * @fileName:截图文件名
 * @beginDate:页面应传参数: 开始时间
 * @endDate:页面应传参数: 结束时间
 * */
function defaultConfig() {
  var fileName = day; //截图文件名
  var shotHour = 8;   //开始截图的小时，每天8点开始截图，十分钟截图一次
  var beginDate = year+'-'+(month>=10?month:'0'+month)+'-'+(day>=10?day:'0'+day)//页面应传参数: 开始时间
  var endDate = year+'-'+(month>=10?month:'0'+month)+'-'+(day>=10?day:'0'+day)//页面应传参数: 结束时间
  if(hour == shotHour && minutes >= 0){
    fileName = 'day'
    beginDate = false
    endDate = false
  }
  if(week == 5 && hour == shotHour && minutes >= 10) {
    fileName = 'week'
    beginDate = dateObj.getLastWeekTime().dateStr
    endDate = year+'-'+(month>=10?month:'0'+month)+'-'+(day>=10?day:'0'+day) //页面应传参数: 结束时间
  }
  if(day  == 1 && hour == shotHour && minutes >= 20){
    fileName = 'month'
    beginDate = dateObj.getLastMonthTime().firstDay
    endDate   = dateObj.getLastMonthTime().lastDay
  }
  if(month == 6 && day == 1 && hour == shotHour && minutes >= 30) {
    fileName = 'firstHalf'
    beginDate = year + '-01-01'
    endDate = year+'06-30'
  }
  if(month == 1 && day == 1 && hour == shotHour && minutes >= 40) {
    fileName = 'secondHalf'
    beginDate = (year-1) + '-07-01'
    endDate = (year-1)+'12-31'
  }
  // if(month == 1 && day == 1 && hour == shotHour && minutes >= 50) {
  //   fileName = 'year'
  //   beginDate = (year-1) + '-01-01'
  //   endDate = (year-1)+'12-31'
  // }
  var obj = {
    fileName:fileName,
    beginDate:beginDate,
    endDate:endDate
  }
  return obj
}
